<?php
/**
 * user: daishiqi
 * Date：2021/6/17
 * Time: 3:41 下午
 */

namespace App\Http\Model\Third;


use App\Http\Model\Hint;
use App\Http\Model\User;
use App\Lib\rc;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use RongCloud\RongCloud;

class AudioUserFollow extends Model
{
    protected $table = 'audio_user_follow';
    protected $guarded = [];

    /**
     * @param $type
     * @param $openId
     * @param $targetUserId
     * @return int
     * @desc 关注/取消关注
     */
    public static function audioUserFollow($type, $openId, $targetUserId)
    {
        $userInfo = (new User())->get_user_by_uid($openId);
        if (empty($userInfo)) {
            return Hint::USER_NOT_EXIST;
        }
        //关注
        if ($type) {
            $data = [
                'target_user_id' => $targetUserId,
                'uuid' => $openId,
                'user_id' => $userInfo['user_id'],
            ];
            self::query()->create($data);
        }

        //取消关注
        if (!$type) {
            self::query()->where(['uuid' => $openId, 'target_user_id' => $targetUserId])->delete();
        }
        return Hint::SUCCESS;

    }

    public static function startLive($acId, $acNickname, $roomId, $roomName)
    {
        $userList = self::query()->where(['target_user_id' => $acId])->get()->toArray();
        if (empty($userList)) {
            Log::info('用户不存在');
            return Hint::SUCCESS;
        }
        $users = array_column($userList, 'user_id');

        $data = [
            'k' => 40001,
            'rid' => $roomId,
            "rn" => $roomName,
            "uid" => $acId,
            "nn" => $acNickname,
            "type" => 1,
        ];
        $rc = new rc();
        $status = $rc->send_system_msg(11, $users, 'kk:room_start', $data, $acNickname . '开播了～赶快进来', 'yyaudio://room/info?id='.$roomId.'&type=1');
        if ($status) {
            return Hint::SUCCESS;
        }
        return Hint::ERROR;
    }


}